package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.pojo.*;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单管理
 */
@RestController
@RequestMapping("order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 员工按条件分页查询订单
     *
     * @param curPage
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("page")
    public R<Page<Orders>> queryOrderByPage(@RequestParam("page") Integer curPage,
                                            Integer pageSize, String number,
                                            String beginTime, String endTime) {
        // 设置分页对象
        Page<Orders> page = new Page<>(curPage, pageSize);
        ordersService.lambdaQuery()
                .like(StringUtils.isNotBlank(number), Orders::getNumber, number)
                .ge(StringUtils.isNotBlank(beginTime), Orders::getOrderTime, beginTime)
                .le(StringUtils.isNotBlank(endTime), Orders::getOrderTime, endTime)
                .orderByDesc(Orders::getOrderTime)
                .page(page);
        return R.success(page);
    }

    /**
     * 员工修改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> modifyOrderStatus(@RequestBody Orders orders) {
        ordersService.updateById(orders);
        return R.success("订单状态修改成功");
    }

    /**
     * 分页查询用户的订单
     *
     * @param curPage
     * @param pageSize
     * @return
     */
    @GetMapping("userPage")
    public R<Page<OrdersDto>> queryUserOrdersByPage(@RequestParam("page") Integer curPage,
                                                    Integer pageSize) {
        // 获取当前登录用户id
        Long userId = BaseContext.getCurrentId();
//        Page<Orders> page = new Page<>(curPage, pageSize);
//        ordersService.lambdaQuery()
//                .eq(Orders::getUserId, userId)
//                .orderByDesc(Orders::getOrderTime)
//                .page(page);
//        Page<OrdersDto> page = ordersService.queryUserOrderByPage(curPage, pageSize, userId);
        Page<OrdersDto> page = ordersService.queryUserOrderNestDetail(curPage, pageSize, userId);
        return R.success(page);
    }

    /**
     * 用户提交订单
     * @param ordersDto
     * @return
     */
    @PostMapping("submit")
    public R<String> submitOrder(@RequestBody OrdersDto ordersDto) {
        // 调用service层下单方法
        ordersService.submitOrder(ordersDto);
        return R.success("用户提交订单成功");
    }

    /**
     * 再来一单
     * @param ordersDto
     * @return
     */
    @PostMapping("again")
    public R<String> submitOrderAgain(@RequestBody OrdersDto ordersDto) {
        ordersService.submitOrderAgain(ordersDto.getId());
        return R.success("再来一单下单成功");
    }
}
